package filters;

import draw.ExploringMedics;

public class LaplasColorFilter extends AbstractColorFilter {

    final static int maskStar[][] = {
        {0, -1, 0},
        {-1, 4, -1},
        {0, -1, 0}
    };
    final static int maskDiag[][] = {
        {-1, -1, -1},
        {-1, 8, -1},
        {-1, -1, -1}
    };
    public static final int TYPE_STAR = 1;
    public static final int TYPE_DIAG = 2;
    int TYPE = TYPE_STAR;
    int correctedImage[][] = null;

    public LaplasColorFilter(int[][] imageDescription) {
        super(imageDescription);
        correctedImage = new int[width][height];
    }

    @Override
    public void doFilted() {
        for (int i = 1; i < width - 1; i++) {
            for (int j = 1; j < height - 1; j++) {
                correctedImage[i][j] = imageDescription[i][j] + convolution(i, j);
                if (correctedImage[i][j] > ExploringMedics.WHITE) {
                    correctedImage[i][j] = ExploringMedics.WHITE;
                }
                if (correctedImage[i][j] < ExploringMedics.BLACK) {
                    correctedImage[i][j] = ExploringMedics.BLACK;
                }
            }
        }
    }

    int convolution(int x, int y) {
        int mask[][] = null;
        switch (TYPE) {
            case TYPE_STAR:
                mask = maskStar;
                break;
            case TYPE_DIAG:
                mask = maskDiag;
                break;
        }
        int answ = 0;
        for (int i = x - 1; i <= x + 1; i++) {
            for (int j = y - 1; j <= y + 1; j++) {
                answ += imageDescription[i][j] * mask[i - x + 1][j - y + 1];
            }
        }

        return answ;
    }

    @Override
    public int[][] getImageDescription() {
        return correctedImage;
    }

    public void setMask(final int TYPE) {
        this.TYPE = TYPE;
    }
}
